package io.persimmon.springboot;

import io.persimmon.core.module.PersimmonModule;
import io.persimmon.core.module.schedule.ScheduleTaskFactory;
import io.persimmon.springboot.strategy.StrategyManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PersimmonSpringContext implements ApplicationContextAware, DisposableBean {

    private final static PersimmonModule persimmonModule = new PersimmonModule();
    private ApplicationContext applicationContext;

    public PersimmonModule getPersimmonModule() {
        return persimmonModule;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public StrategyManager getStrategyManager() {
        return applicationContext.getBean(StrategyManager.class);
    }

    @Override
    public void destroy() throws Exception {
        log.info("PersimmonSpringContext start destroy");

        ScheduleTaskFactory.stop();
        log.info("destroy scheduleTaskFactory done.");

        log.info("PersimmonSpringContext finish destroy");
    }
}
